package moviles.comprap;

import java.util.List;
import java.util.Map;

import moviles.mundo.Comprap;
import moviles.mundo.ItemProductoLista;
import moviles.mundo.ItemServicioLista;
import moviles.mundo.Lista;

import com.example.comprap.R;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListAdapterLista extends BaseExpandableListAdapter{
	 
	private Activity context;
	private Map<String, List<String>> ListasCollections;
	private List<String> listas;
	private String listaSelected;
	private String itemSelected;
	 
	    public ExpandableListAdapterLista(Activity context, List<String> listas,
	            Map<String, List<String>> ListasCollections) {
	        this.context = context;
	        this.ListasCollections = ListasCollections;
	        this.listas = listas;
	        listaSelected = "";
	        itemSelected = "";
	    }
	 
	    public Object getChild(int groupPosition, int childPosition) {
	        return ListasCollections.get(listas.get(groupPosition)).get(childPosition);
	    }
	 
	    public long getChildId(int groupPosition, int childPosition) {
	        return childPosition;
	    }
	 
	    public View getChildView(final int groupPosition, final int childPosition,
	            boolean isLastChild, View convertView, ViewGroup parent) {
	        final String lista = (String) getChild(groupPosition, childPosition);
	        LayoutInflater inflater = context.getLayoutInflater();
	 
	        if (convertView == null) {
	            convertView = inflater.inflate(R.layout.child_item, null);
	        }
	 
	        TextView item = (TextView) convertView.findViewById(R.id.text);
	 
	        ImageView delete = (ImageView) convertView.findViewById(R.id.delete);
	        delete.setOnClickListener(new OnClickListener() {
	 
	            public void onClick(View v) {
	                AlertDialog.Builder builder = new AlertDialog.Builder(context);
	                builder.setMessage("Desea borrar este item?");
	                builder.setCancelable(false);
	                builder.setPositiveButton("Si",
	                        new DialogInterface.OnClickListener() {
	                            public void onClick(DialogInterface dialog, int id) {
	                                
	                            	List<String> child =	                                		
	                                ListasCollections.get(listas.get(groupPosition));	                                
	                                
	                                listaSelected = listas.get(groupPosition);
	                                itemSelected = ListasCollections.get(listas.get(groupPosition)).get(childPosition);
	                             
	                                Lista listaseleccionada =Comprap.darInstancia().buscarLista(listaSelected);
	                                ItemProductoLista itemProd = listaseleccionada.buscarProducto(itemSelected.split(": ")[1]);
	                                ItemServicioLista itemSer = listaseleccionada.buscarServicio(itemSelected.split(": ")[1]);
	                                
	                                if(itemProd!=null){ 
	                                Comprap.darInstancia().removeProductoLista(listaseleccionada,itemProd); 
	                                Toast.makeText(context,"Producto Eliminado", Toast.LENGTH_SHORT).show();}
	                                if(itemSer!=null){ Comprap.darInstancia().removeServicioLista(listaseleccionada,itemSer); 
	                                Toast.makeText(context,"Servicio Eliminado", Toast.LENGTH_SHORT).show();}
	                                //Notifica que la lista se actualizo!
	                                child.remove(childPosition);
	                                notifyDataSetChanged();
	                                
	                            }
	                        });
	                builder.setNegativeButton("No",
	                        new DialogInterface.OnClickListener() {
	                            public void onClick(DialogInterface dialog, int id) {
	                                dialog.cancel();
	                            }
	                        });
	                AlertDialog alertDialog = builder.create();
	                alertDialog.show();	            }
	        });
	 
	        item.setText(lista);
	        return convertView;
	    }
	 
	    public int getChildrenCount(int groupPosition) {
	        return ListasCollections.get(listas.get(groupPosition)).size();
	    }
	 
	    public Object getGroup(int groupPosition) {
	        return listas.get(groupPosition);
	    }
	 
	    public int getGroupCount() {
	        return listas.size();
	    }
	 
	    public long getGroupId(int groupPosition) {
	        return groupPosition;
	    }
	 
	    public View getGroupView(int groupPosition, boolean isExpanded,
	            View convertView, ViewGroup parent) {
	        String nombre = (String) getGroup(groupPosition);
	        if (convertView == null) {
	            LayoutInflater infalInflater = (LayoutInflater) context
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = infalInflater.inflate(R.layout.list_item,
	                    null);
	        }
	        TextView item = (TextView) convertView.findViewById(R.id.nombre);
	        item.setTypeface(null, Typeface.BOLD);
	        item.setText(nombre );
	        
	        TextView precio = (TextView) convertView.findViewById(R.id.precio);
			if(precio != null) {
				 listaSelected = listas.get(groupPosition);
				precio.setText("Precio Total : " + Comprap.darInstancia().buscarLista(listaSelected).calcularPrecio() );
			}	        
	        return convertView;
	    }
	 
	    public boolean hasStableIds() {
	        return true;
	    }
	 
	    public boolean isChildSelectable(int groupPosition, int childPosition) {
	        return true;
	    }
	}